﻿using System;
using System.Collections.Generic;

namespace InfiniTec
{
	public static class EnumerableExtension
	{
		public static void Foreach<T>(this IEnumerable<T> items, Action<T> action)
		{
			if (items == null) throw new ArgumentNullException("items");
			if (action == null) throw new ArgumentNullException("action");

			foreach (var item in items)
			{
				action(item);
			}
		}

		public static void Foreach<TItem, TException>(this IEnumerable<TItem> items, Action<TItem> action, Action<TItem, TException> perItemExceptionHandler)
			where TException: Exception
		{
			if (items == null) throw new ArgumentNullException("items");
			if (action == null) throw new ArgumentNullException("action");
			if (perItemExceptionHandler == null) throw new ArgumentNullException("perItemExceptionHandler");

			foreach (var item in items)
			{
				try
				{
					action(item);
				}
				catch (TException ex)
				{
					perItemExceptionHandler(item, ex);
				}
			}
		}
	}
}
